package com.ruoyi.robot.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.robot.pojo.po.Essay;
import com.ruoyi.robot.mapper.EssayMapper;
import com.ruoyi.robot.pojo.dto.EssayDto;
import com.ruoyi.robot.service.EssayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.robot.util.DtoToPoMapper;
import com.ruoyi.robot.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xds
 * @since 2023-01-16
 */
@Service
public class EssayServiceImpl extends ServiceImpl<EssayMapper, Essay> implements EssayService {
    @Autowired
    private EssayMapper essayMapper;

    /**
     * 查询文章表
     *
     * @param essayId 文章表主键
     * @return 文章表
     */
    @Override
    public Essay selectEssayByEssayId(Long essayId)
    {
        return essayMapper.selectById(essayId);
    }

    /**
     * 查询文章表列表
     *
     * @param essayDto 文章表
     * @return 文章表
     */
    @Override
    public List<Essay> selectEssayList(EssayDto essayDto)
    {
        Essay essay = DtoToPoMapper.INSTANCE.toEssay(essayDto);
        return essayMapper.selectEssayList(essay);
    }

    /**
     * 新增文章表
     *
     * @param essayDto 文章表
     * @return 结果
     */
    @Override
    public int insertEssay(EssayDto essayDto)
    {
        Essay essay = DtoToPoMapper.INSTANCE.toEssay(essayDto);
        essay.setEssayTime(LocalDateTime.now());
        return essayMapper.insert(essay);
    }

    /**
     * 修改文章表
     *
     * @param essayDto 文章表
     * @return 结果
     */
    @Override
    public int updateEssay(EssayDto essayDto)
    {
        Essay essay = DtoToPoMapper.INSTANCE.toEssay(essayDto);
        return essayMapper.updateById(essay);
    }

    /**
     * 批量删除文章表
     *
     * @param essayIds 需要删除的文章表主键
     * @return 结果
     */
    @Override
    public int deleteEssayByEssayIds(Long[] essayIds)
    {
        return essayMapper.deleteEssayByEssayIds(essayIds);
    }

    /**
     * 删除文章表信息
     *
     * @param essayId 文章表主键
     * @return 结果
     */
    @Override
    public int deleteEssayByEssayId(Long essayId)
    {
        return essayMapper.deleteById(essayId);
    }

    @Override
    public AjaxResult uploadPicture(MultipartFile multipartFile) {
        if(multipartFile == null){
            return AjaxResult.error("上传文件不能为空");
        }
//       FileUploadUtil.upload(multipartFile,BASE_DIR);
        String musicUrl = FileUploadUtil.upload(multipartFile, Constants.BASE_DIR);
        String picture = Constants.RETURN_BASE_DIR+musicUrl;
        return AjaxResult.success("上传成功",picture);
    }

}
